package com.chat.controller;

import cn.hutool.core.util.ObjectUtil;
import com.chat.domain.dto.ChatQueryDTO;
import com.chat.domain.pojo.User;
import com.chat.service.GroupChatService;
import com.chat.service.UserService;
import com.chat.util.SecurityHolderUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 页面控制器
 *
 * @author wb
 * @since 2024-08-20
 */

@Controller
public class PageController {

    @Value("${netty.ws}")
    private String ws;

    @Autowired
    private UserService userService;

    @Autowired
    private GroupChatService groupChatService;


    // 登录页面
    @GetMapping({"/", "/login", "/index"})
    public String login() {
        return "login";
    }

    // 内容
    @RequestMapping("/content")
    public String content(Model model, HttpSession session) {
        // 当前登录用户的ws地址
        model.addAttribute("ws", ws);

        ChatQueryDTO chatQueryDTO = new ChatQueryDTO();
        chatQueryDTO.setUserId(SecurityHolderUtil.getUserId(session));

        // 聊天信息
        model.addAttribute("chats", userService.getChats(chatQueryDTO));

        return "main";
    }

    // 主页
    @RequestMapping("/chat")
    public String index(HttpSession session) {
        // 检查用户是否登录
        User user = SecurityHolderUtil.getUser(session);
        if (ObjectUtil.isEmpty(user)) {
            return "login";
        }

        return "index";
    }

    // 通讯录
    @GetMapping("/contact")
    public String contact() {
        return "contact";
    }

    // 详情
    @GetMapping("/detail/{chatId}")
    public String detail(@PathVariable String chatId, Model model, HttpSession session) {
        if (chatId.contains("user")) {
            User user = userService.getById(getUserId(chatId, session));
            model.addAttribute("user", user);
        } else if (chatId.contains("group")) {
            String groupId = chatId.substring(chatId.indexOf("-") + 1);
            model.addAttribute("group", groupChatService.getById(groupId));
        }

        return "detail";
    }

    private static String getUserId(String chatId, HttpSession session) {
        Integer userId = SecurityHolderUtil.getUserId(session);

        String str = chatId.substring(chatId.indexOf("-") + 1);
        String[] arr = str.split("-");

        for (String s : arr) {
            if (!s.equals(userId.toString())) {
                return s;
            }
        }

        return null;
    }

    // 个人信息页面
    @GetMapping("/profile")
    public String updateProfile() {
        return "profile";
    }


    // 退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        // 销毁 session
        session.invalidate();
        return "login";
    }
}
